package com.cat.model.biz.service.bs.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alonew.db.dao.TxCommonDAO;
import com.cat.model.biz.logic.BasLogic;
import com.cat.model.biz.service.bs.SmsBS;
import com.cat.model.common.model.common.BasConfigVO;
import com.cat.model.common.model.common.PlmSmsVO;
import com.cat.model.common.util.Constant;
import com.cat.model.common.util.LoggerManager;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.request.AlibabaAliqinFcTtsNumSinglecallRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import com.taobao.api.response.AlibabaAliqinFcTtsNumSinglecallResponse;

/**
 * 
 * 
 */
@Service("smsBS")
public class SmsBSImpl implements SmsBS{

	private Logger logger = LoggerFactory.getLogger(SmsBSImpl.class);
	
	@Autowired
	private TxCommonDAO txCommonDAO;
	
	@Autowired
	private BasLogic basLogic;
	                             

    //阿里大鱼
    private static final String Url = "http://gw.api.taobao.com/router/rest";
    private static final String AppKey           = "23537619";
    private static final String AppSecret         = "eb8b79e287cf083d419f146d8b425454";         
    private static final boolean model_debug=false;
    
    /**
     * 订单支付成功短信通知
     * 订单已支付成功！客户账号：${phone}；预约工作时间：${time}；预订模特编码：${model}。易选模APP
     */
    public void sendSMSpayOk(String sendPhone, String phone, String time,String model) {
    	String ttsPrams = "{\"phone\":\""+phone+"\",\"time\":\""+time+"\",\"model\":\""+model+"\"}";
    	saveSms(sendPhone, ttsPrams, PlmSmsVO.TYPE_TAOBAO_SMS);
    	if(model_debug){
    		return ;
    	}else{
    		TaobaoClient client = new DefaultTaobaoClient(Url, AppKey, AppSecret);
        	AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        	req.setSmsType("normal");
        	req.setSmsParam(ttsPrams);
        	req.setRecNum(String.valueOf(sendPhone));
        	
        	req.setSmsFreeSignName("易选模APP");
        	req.setSmsTemplateCode("SMS_26160230");
        	
        	BasConfigVO signConfig = basLogic.getOneConfigByName(Constant.config_plm_SmsFreeSignName);
        	if(signConfig != null && StringUtils.isNotEmpty(signConfig.getConfigValue())){
        		req.setSmsFreeSignName(signConfig.getConfigValue());
        	}
        	
        	BasConfigVO tplConfig = basLogic.getOneConfigByName(Constant.config_plm_SmsTemplateCode);
        	if(tplConfig != null && StringUtils.isNotEmpty(tplConfig.getConfigValue())){
        		req.setSmsTemplateCode(tplConfig.getConfigValue());
        	}
        	
        	try {
        		AlibabaAliqinFcSmsNumSendResponse response = client.execute(req);
        		LoggerManager.sms.error("sendCallBYTaobaoResult|" + response.getBody());
    		} catch (ApiException e) {
    			LoggerManager.sms.error("sendCallBYTaobaoError|" + e.getMessage(), e);
    		}
    	}
    	
    }
  
    
    /**
     * 预约单短信通知
     * 易选模APP有新的预约订单。客户账号：${phone}；预约工作时间：${time}；预订模特编码：${model}。
     */
    public void sendSMSReservation(String sendPhone, String phone, String time,String model) {
    			
    	String ttsPrams = "{\"phone\":\""+phone+"\",\"time\":\""+time+"\",\"model\":\""+model+"\"}";
    	saveSms(sendPhone, ttsPrams, PlmSmsVO.TYPE_TAOBAO_SMS);
    	
    	if(model_debug){
    		return ;
    	}else{
    		TaobaoClient client = new DefaultTaobaoClient(Url, AppKey, AppSecret);
        	AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        	req.setSmsType("normal");
        	req.setSmsParam(ttsPrams);
        	req.setRecNum(String.valueOf(sendPhone));
        	
        	req.setSmsFreeSignName("易选模APP");
        	req.setSmsTemplateCode("SMS_26760037");
        	
        	BasConfigVO signConfig = basLogic.getOneConfigByName(Constant.config_plm_SmsFreeSignName);
        	if(signConfig != null && StringUtils.isNotEmpty(signConfig.getConfigValue())){
        		req.setSmsFreeSignName(signConfig.getConfigValue());
        	}
        	
        	BasConfigVO tplConfig = basLogic.getOneConfigByName(Constant.config_plm_SmsTemplateCode);
        	if(tplConfig != null && StringUtils.isNotEmpty(tplConfig.getConfigValue())){
        		req.setSmsTemplateCode(tplConfig.getConfigValue());
        	}
        	
        	try {
        		AlibabaAliqinFcSmsNumSendResponse response = client.execute(req);
        		LoggerManager.sms.error("sendCallBYTaobaoResult|" + response.getBody());
    		} catch (ApiException e) {
    			LoggerManager.sms.error("sendCallBYTaobaoError|" + e.getMessage(), e);
    		}
    	}
    	
    	
    }
    
    
    
    public static void main(String[] args) {
    	SmsBSImpl bsImpl=new SmsBSImpl();
    	bsImpl.sendSMSReservation("18859255859","18859255858", "2016-11-08 00:00:00", "N9527");
	}
    

    
    @Override
    public String sendCheckCodeSMS(String phone, String checkCode) {    	    	
    	sendSMSBYTaobao(phone, checkCode);
    	return "";
    }

    public void sendSMSBYTaobao(String phone, String checkCode) {
    	saveSms(phone, checkCode, PlmSmsVO.TYPE_TAOBAO_SMS);
    	
    	String ttsPrams = "{\"code\":\""+checkCode+"\",\"product\":\"易选模\"}";
    	
    	TaobaoClient client = new DefaultTaobaoClient(Url, AppKey, AppSecret);
    	AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
    	req.setSmsType("normal");
    	req.setSmsParam(ttsPrams);
    	req.setRecNum(String.valueOf(phone));
    	
    	req.setSmsFreeSignName("易选模APP");
    	req.setSmsTemplateCode("SMS_13220858");
    	
    	BasConfigVO signConfig = basLogic.getOneConfigByName(Constant.config_plm_SmsFreeSignName);
    	if(signConfig != null && StringUtils.isNotEmpty(signConfig.getConfigValue())){
    		req.setSmsFreeSignName(signConfig.getConfigValue());
    	}
    	
    	BasConfigVO tplConfig = basLogic.getOneConfigByName(Constant.config_plm_SmsTemplateCode);
    	if(tplConfig != null && StringUtils.isNotEmpty(tplConfig.getConfigValue())){
    		req.setSmsTemplateCode(tplConfig.getConfigValue());
    	}
    	
    	try {
    		AlibabaAliqinFcSmsNumSendResponse response = client.execute(req);
    		LoggerManager.sms.error("sendCallBYTaobaoResult|" + response.getBody());
		} catch (ApiException e) {
			LoggerManager.sms.error("sendCallBYTaobaoError|" + e.getMessage(), e);
		}
    }
    
    @Override
    public void sendCallBYTaobao(String phone, String checkCode) {
    	saveSms(phone, checkCode, PlmSmsVO.TYPE_TAOBAO_CALL);
    	
    	String ttsPrams = "{\"code\":\""+checkCode+"\",\"product\":\"柚看\"}";
    	
    	TaobaoClient client = new DefaultTaobaoClient(Url, AppKey, AppSecret);
    	AlibabaAliqinFcTtsNumSinglecallRequest req = new AlibabaAliqinFcTtsNumSinglecallRequest();
    	req.setTtsParam(ttsPrams);
    	req.setCalledNum(String.valueOf(phone));
    	
    	req.setCalledShowNum("4008816150");
    	req.setTtsCode("TTS_7771034");
    	
    	BasConfigVO showNumConfig = basLogic.getOneConfigByName(Constant.config_plm_CalledShowNum);
    	if(showNumConfig != null && StringUtils.isNotEmpty(showNumConfig.getConfigValue())){
    		req.setCalledShowNum(showNumConfig.getConfigValue());
    	}
    	
    	BasConfigVO ttsConfig = basLogic.getOneConfigByName(Constant.config_plm_TtsCode);
    	if(ttsConfig != null && StringUtils.isNotEmpty(ttsConfig.getConfigValue())){
    		req.setTtsCode(ttsConfig.getConfigValue());
    	}
    	
    	try {
			AlibabaAliqinFcTtsNumSinglecallResponse response = client.execute(req);
			LoggerManager.sms.error("sendCallBYTaobaoResult|" + response.getBody());
		} catch (ApiException e) {
			LoggerManager.sms.error("sendCallBYTaobaoError|" + e.getMessage(), e);
		}
    }
    
	private PlmSmsVO saveSms(String phone, String encodedMsg, String type) {
		PlmSmsVO sms = new PlmSmsVO();
    	sms.setPhoneNo(String.valueOf(phone));
    	sms.setMessage(encodedMsg);
    	sms.setCreateTime(new Date());
    	sms.setType(type);
    	
    	txCommonDAO.insert(sms);
    	
    	logger.info("Sent code|" + phone);
		return sms;
	}
    
}
